﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;

namespace LibreriaCommon
{
    public class Cuit
    {
        //Atributos Privados
        private String prefix = "";
        private String number = "";
        private String subfix = "";
        private Regex rg = new Regex("[A-Z_a-z]");

        // Constructores
        public Cuit()
        {
        }

        public Cuit(String cuit)
        {
            this.setCuit(cuit);
        }

        public Cuit(String prefix, String number, String subfix)
        {
            Prefix = prefix;
            Number = number;
            Subfix = subfix;
        }

        // Getters y Setters
        public String Prefix
        {
            get
            {
                return prefix;
            }
            set
            {
                if (value.Length == 2 && !rg.IsMatch(value))
                    prefix = value;
                else
                    prefix = "";
            }
        }

        public String Number
        {
            get
            {
                return number;
            }
            set
            {
                if (value.Length == 8 && !rg.IsMatch(value))
                    number = value;
                else
                    number = "";
            }
        }

        public String Subfix
        {
            get
            {
                return subfix;
            }
            set
            {
                if (value.Length == 1 && !rg.IsMatch(value))
                    subfix = value;
                else
                    subfix = "";
            }
        }

        public void setCuit(string cuit)
        {
            cuit = cuit.Replace("-", "");
            if (cuit.Length == 11)
            {
                Prefix = cuit.Substring(0, 2);
                Number = cuit.Substring(2, 8);
                Subfix = cuit.Substring(10, 1);
            }            
        }

        // Metodos Publicos
        public override String ToString()
        {
            return (Prefix + '-' + Number + '-' + Subfix);
        }

        public bool Equals(Cuit cuit)
        {
            if (!(cuit.isValid() && this.isValid()))
                return false;

            if (!cuit.Prefix.Equals(this.Prefix))
                return false;

            if (!cuit.Number.Equals(this.Number))
                return false;

            if (!cuit.Subfix.Equals(this.Subfix))
                return false;

            return true;
        }

        public bool isValid()
        {
            String cuit = Prefix + Number + Subfix;
            return ValidarString(cuit);
        }

        public static bool isValid(String cuit)
        {
            return ValidarString(cuit.Replace("-", ""));
        }

        // Metodos Privadas
        private static bool ValidarString(String cuit)
        {
            string cuitTemp = cuit.Replace("-", "");
            long suma = 0;
            long resto;
            string[] pos;
            string coef = "5432765432";
            
            pos = cuitTemp.Split();

            if (cuitTemp.Length != 11)
                return false;

            long cuitLong;

            if (!long.TryParse(cuitTemp, out cuitLong))
                return false;

            for (int i = 0; i < 10; i++)
            {
                suma += long.Parse(cuitTemp.Substring(i, 1)) * int.Parse(coef.Substring(i, 1));
            }

            resto = suma % 11;

            if (resto > 1) 
                resto = 11 - resto; 

            if (resto == long.Parse(cuitTemp.Substring(cuit.Length - 1)))
                return true;
            else
                return false;
        }
    }
}
